package cn.pupil.design;

import cn.pupil.design.event.EventManager;
import cn.pupil.design.event.listener.MQEventListener;
import cn.pupil.design.event.listener.MessageEventListener;

/**
 * @Description: 摇号服务抽象类，也可以创建接口，在用抽象类实现
 * @Author: pupil
 * @Date: 2024/07/30 下午 6:41
 */
public abstract class LotteryService{

    // 获取监听管理类
    private EventManager eventManager;

    public LotteryService() {
        // 初始化监听管理类
        eventManager = new EventManager(EventManager.EventType.MQ,EventManager.EventType.Message);
        // 添加监听事件
        eventManager.subscribe(EventManager.EventType.MQ,new MQEventListener());
        eventManager.subscribe(EventManager.EventType.Message,new MessageEventListener());
    }

    /**
     * 业务处理方法
     * @param uId 用户id
     * @return
     */
    public LotteryResult draw(String uId) {
        LotteryResult lotteryResult = doDraw(uId);
        // 需要什什么通知就给调⽤用什什么⽅方法
        /*eventManager.notify(EventManager.EventType.MQ,lotteryResult);
        eventManager.notify(EventManager.EventType.Message,lotteryResult);*/

        // 通知调用所有事件
        eventManager.notifyAll(lotteryResult);
        return lotteryResult;
    }

    /**
     * 核心业务处理
     * @param uId 用户id
     * @return
     */
    protected abstract LotteryResult doDraw(String uId);
}
